from django.db import models

# Create your models here.

from django.db import models
from mlh.utils.models import BaseModel
from users.models import User


class LabelModel(models.Model):
    """标签及详情表"""
    label_name = models.CharField(max_length=64, verbose_name='标签名')
    label_content = models.TextField(default='', verbose_name='标签详情')
    label_attennum = models.IntegerField(default=0, verbose_name='关注用户数')

    class Meta:
        db_table = 'tb_answers_lables'
        verbose_name = '标签详情表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.label_name


class LabelUserModel(models.Model):
    """用户标签关注表"""
    label = models.ForeignKey(LabelModel, on_delete=models.CASCADE, verbose_name='标签序号')
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='用户序号')

    class Meta:
        db_table = 'tb_answers_LabelUser'
        verbose_name = '用户标签关注表'
        verbose_name_plural = verbose_name


class QuesUserModel(BaseModel):
    """用户问题关注表"""
    ques = models.ForeignKey(LabelModel, on_delete=models.CASCADE, verbose_name='问题序号')
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='用户序号')

    class Meta:
        db_table = 'tb_answers_QuesUser'
        verbose_name = '用户问题关注表'
        verbose_name_plural = verbose_name


class QuesModel(BaseModel):
    """问题表"""
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='创建用户')  # 外键
    title = models.CharField(max_length=128, verbose_name='标题')
    label = models.ForeignKey(LabelModel, related_name='quest_labels', verbose_name='标签', on_delete=models.CASCADE)
    content = models.TextField(verbose_name='问题详情')

    class Meta:
        db_table = 'tb_answers_questions'
        verbose_name = '问题详情表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.title


class QattrModel(models.Model):
    """问题相关属性表"""
    ques = models.ForeignKey(QuesModel, verbose_name='问题', on_delete=models.CASCADE)
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    answer_count = models.IntegerField(default=0, verbose_name='回答数')
    click_count = models.IntegerField(default=0, verbose_name='点击量')
    report_count = models.IntegerField(default=0, verbose_name='举报量')
    new_answer_time = models.DateTimeField(null=True, auto_now=False, verbose_name='最新回答时间')
    answer = models.ForeignKey(User, on_delete=models.CASCADE, related_name='answer_name', verbose_name='最新回答用户',
                               null=True)

    class Meta:
        db_table = 'tb_answers_Qattr'
        verbose_name = '问题相关属性表'
        verbose_name_plural = verbose_name


class QuesLabelModel(models.Model):
    """问题与标签关联表"""
    ques = models.ForeignKey(QuesModel, verbose_name='问题', on_delete=models.CASCADE)
    label = models.ForeignKey(LabelModel, verbose_name='标签', on_delete=models.CASCADE)

    class Meta:
        db_table = 'tb_answers_quest_label'
        verbose_name = '问题标签关联表'
        verbose_name_plural = verbose_name


class ReportQuestModel(models.Model):
    """用户举报问题表"""
    ques = models.ForeignKey(QuesModel, verbose_name='问题', on_delete=models.CASCADE)
    user = models.ForeignKey(User, verbose_name='用户', on_delete=models.CASCADE)
    report = models.BooleanField(default=False, verbose_name='举报')

    class Meta:
        db_table = 'tb_answers_report'
        verbose_name = '用户举报问题表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.ques


class CommentdetailModel(models.Model):
    """评论详情表"""
    report_count = models.IntegerField(default=0, verbose_name="举报数量")
    like_count = models.IntegerField(default=0, verbose_name="点赞数")
    hate_count = models.IntegerField(default=0, verbose_name="踩数")
    ccontent = models.TextField(default='', verbose_name="内容")

    class Meta:
        db_table = 'tb_answers_comment_quest_detail'
        verbose_name = '评论详情表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.ccontent


class CommentModel(BaseModel):
    """评论表"""
    ques = models.ForeignKey(QuesModel, verbose_name="问题", on_delete=models.CASCADE)
    user = models.ForeignKey(User, verbose_name="评论用户", on_delete=models.CASCADE)
    content = models.ForeignKey(CommentdetailModel, verbose_name='评论内容表', on_delete=models.CASCADE)

    class Meta:
        db_table = 'tb_answers_comment_quest'
        verbose_name = '评论内容表'
        verbose_name_plural = verbose_name
        ordering = ['-create_time']


class SecondcommentModel(BaseModel):
    """次评论表"""
    comment = models.ForeignKey(CommentModel, verbose_name='评论', on_delete=models.CASCADE)
    user = models.ForeignKey(User, verbose_name="评论用户", on_delete=models.CASCADE)
    cdetail = models.ForeignKey(CommentdetailModel, verbose_name='评论详情', on_delete=models.CASCADE)

    class Meta:
        db_table = 'tb_answers_second_comment'
        verbose_name = '次级评论表'
        verbose_name_plural = verbose_name


class ReportcommentModel(models.Model):
    """用户举报、点赞、讨厌评论详情表"""
    cdeatil = models.ForeignKey(CommentdetailModel, verbose_name='评论内容', on_delete=models.CASCADE)
    user = models.ForeignKey(User, verbose_name='评论内容', on_delete=models.CASCADE)
    is_like = models.IntegerField(default=-1, verbose_name='点赞')
    is_hate = models.IntegerField(default=-1, verbose_name='踩')
    is_report = models.IntegerField(default=-1, verbose_name='举报')

    class Meta:
        db_table = 'tb_answers_comment_report_user'
        verbose_name = '用户举报评论表'
        verbose_name_plural = verbose_name
